package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.feign.ApBehaviorClient;
import com.usian.article.mapper.ApCollectionMapper;
import com.usian.article.service.ApCollectionService;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApCollection;
import com.usian.model.behavior.dtos.CollectionBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName: ApCollectionServiceImpl
 * Author:   WGG
 * Date:    2022-12-2022/12/1-20:38
 * Version: 1.0
 * Description:
 */
@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {

    @Autowired
    private ApBehaviorClient apBehaviorEntryClient;

    @Override
    public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {
        //判空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //拿到登录id
        ApUser user = AppThreadLocalUtils.getUser();
        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }
        //如果未登录不能收藏
        if (dto.getEquipmentId()==null||dto.getEquipmentId()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据登录id查看行为实体数据是否存在
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryClient.showBehavior(user.getId(),dto.getEquipmentId());
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询收藏数据是否存在
        ApCollection collection = getOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getEntryId, apBehaviorEntry.getId()));

        if (dto.getOperation()==0&&collection==null){
            //封装数据
            ApCollection apCollection = new ApCollection();
            apCollection.setArticleId(dto.getEntryId());
            apCollection.setCollectionTime(new Date());
            apCollection.setPublishedTime(dto.getPublishedTime());
            apCollection.setEntryId(apBehaviorEntry.getId());
            apCollection.setType(dto.getType());
            //添加
            save(apCollection);
        }else {
            //删除
            removeById(collection.getId());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ApCollection showCollectionBehavior(ArticleInfoDto dto) {
        if (dto!=null){
            return getOne(Wrappers.<ApCollection>lambdaQuery().eq(ApCollection::getArticleId, dto.getArticleId()).eq(ApCollection::getEntryId,dto.getId()));
        }
        return null;
    }
}
